--[[
	循环从 gmmqredis 读取命令， 主要是web端写入，也是以后的所有控制命令的入口，采用mq的异步格式
	gmmqredis: 有三个mq(request, response, notify)，都是json编码
	参数： msgsession, msghead， msgbody ,基本参照protobuf的定义来，分成 head，body两块
	msgsession 由命名传入方保证唯一， msgbody里面需要加入md5的签名。debug模式不考虑
	返回： msgsession msghead(msgname, retcode, retstr) , msgbody
]]

local skynet = require "skynet"
local cluster = require "skynet.cluster"
require "skynet.manager"

local json = require "cjson.safe"
json.encode_sparse_array(true,1,1)

local redisdao = require "redisdao"
local filelog = require "filelog"
local tabletool = require "tabletool"
local configdao = require "configdao"
local timetool = require "timetool"
local os = os

require "pubenum"

local status, cmd, result, logstr

function docmd(cmd)
    local c = cmd
    if cmd ~= "get_online_state" then
    	skynet.send(".gamedatalog", "lua", "write", "-->" .. cmd)
    end
	cmd = json.decode(cmd)
	if not cmd or type(cmd) ~= "table" then 
        cmd = {msghead = {msgname = c},msgbody = {}}
    end

    local result = nil
    local logstr = nil
    local status = false
    filelog.print("line25", cmd)
	
	if cmd.msghead.msgname == "kickrole" then       --强制用户下线
        local rid = cmd.msgbody.rid 
		local status = true
		if not rid then
			status,rid = redisdao.query_data(".gmmqredis", "lpop", "requestmq")
		end
		result = cluster.call("gatenode", ".router", cmd.msghead.msgname, tonumber(rid))
	elseif cmd.msghead.msgname == "renew_rank" then --刷新排行榜
        -- 命令行可以直接 ：redis-cli -n 15 rpush requestmq renew_rank 就可以来简单触发更新排行榜
        if not cmd.msgbody.linenum then
            local status,linenum = redisdao.query_data(".gmmqredis", "lpop", "requestmq")
            cmd.msgbody.linenum = linenum or 10
        end
		if not cmd.msgbody.rankname then
            local ranklist = {[2] = "diamondrank",[3] = "coinrank", [4] = "drrank", [5] = "beaterrank", [6] = "shooterrank", [7] = "linkerrank",}
            for ranktype,rankname in pairs(ranklist) do
                result = skynet.call(".rank", "lua", cmd.msghead.msgname, rankname, ranktype, cmd.msgbody.linenum)
            end
        else
            result = skynet.call(".rank", "lua", cmd.msghead.msgname, cmd.msgbody.rankname, cmd.msgbody.ranktype, cmd.msgbody.linenum)
        end
	elseif cmd.msghead.msgname == "get_online_state" then   --获取在线数据
        _,result = pcall(cluster.call,"gatenode", ".router", cmd.msghead.msgname)
        local nodenames = {"dn", "psz", "ddz"}
        local matchdata = {}
        local stat, data
        for _, name in ipairs(nodenames) do
            stat, data = pcall(cluster.call, name .. "node", ".router", "get_matchroom_count")
            if stat == true and data then
                matchdata[name] = data
            end
        end
        result.matchdata = matchdata
	elseif cmd.msghead.msgname == "setagencylevel" then     --设置代理级别
		if tonumber(cmd.msgbody.agencylevel) > 0 then
			result = cluster.call("gatenode", ".router", "updateroleinfo", cmd.msgbody.rid, {agencylevel=tonumber(cmd.msgbody.agencylevel)})
		end
    elseif cmd.msghead.msgname == "setagencyrid" then       --设置上级代理
		local agencyrid = tonumber(cmd.msgbody.agencyrid) or 0
		result = cluster.call("gatenode", ".router", "updateroleinfo", cmd.msgbody.rid, { agencyrid = tonumber(agencyrid) })
	elseif cmd.msghead.msgname == "newmail" then            --发邮件
        result = cluster.call("gatenode", ".router", cmd.msghead.msgname, cmd.msgbody.rid, cmd.msgbody.fields)
	elseif cmd.msghead.msgname == "set_system_ad" then      --设置广告栏广告
		_,result = pcall(cluster.call,"gatenode", ".router", cmd.msghead.msgname, cmd.msgbody.addata)
		redisdao.query_data(".gmmqredis", "set", "hall_systemad", json.encode(cmd.msgbody.addata))
	elseif cmd.msghead.msgname == "addhrlmsg" then          --添加动态跑马灯
		result = cluster.call("gatenode", ".router", cmd.msghead.msgname, cmd.msgbody.msg)
    elseif cmd.msghead.msgname == "renew_clientver" then    --刷新客户端版本
		result = skynet.call(".clientver", "lua", cmd.msghead.msgname)
    elseif cmd.msghead.msgname == "destroytable"            --删除桌子
    or cmd.msghead.msgname == "set_tableend_flag" then      --设置桌子结束标记
		local code = cmd.msgbody.code 
		local status = true
		if not code then
			status,code = redisdao.query_data(".gmmqredis", "lpop", "requestmq")
		end
		if status and code then
			cluster.send("gatenode", ".router", cmd.msghead.msgname,tonumber(code))
		end
    elseif cmd.msghead.msgname == "get_table_allrid" then   --获得桌子所有玩家rid
		local code = cmd.msgbody.code 
		local status = true
		if not code then
			status,code = redisdao.query_data(".gmmqredis", "lpop", "requestmq")
		end
        if status and code then
		    result = cluster.call("gatenode", ".router", cmd.msghead.msgname,tonumber(code))
        end
	 elseif cmd.msghead.msgname == "update_match_outtime" then  --修改匹配超时时间
		local new_time = cmd.msgbody.new_time 
		local status = true
		if not new_time then
			status,new_time = redisdao.query_data(".gmmqredis", "lpop", "requestmq")
		end
        if status and new_time then
		    cluster.send("mjzznode", ".router", cmd.msghead.msgname,tonumber(new_time))
        end
	elseif cmd.msghead.msgname == "update_min_queue_len" then   --修改匹配队列最小匹配长度
		local new_len = cmd.msgbody.new_len 
		local status = true
		if not new_len then
			status,new_len = redisdao.query_data(".gmmqredis", "lpop", "requestmq")
		end
        if status and new_len then
		    cluster.send("mjzznode", ".router", cmd.msghead.msgname,tonumber(new_len))
        end
	elseif cmd.msghead.msgname == "notify_all_online_roles" then    --全服广播
        if not cmd.msgbody or not cmd.msgbody.msg then
		    local f = io.open("msg.lua","r")
            local source = nil
	        if f then
		        source = f:read "*a"
                f:close()
	        end
	        if source == nil then
		        return
	        end
	        local msg = load(source,"error file!")()
            cluster.send("gatenode", ".router", cmd.msghead.msgname,tostring(msg))
        else
            cluster.send("gatenode", ".router", cmd.msghead.msgname,cmd.msgbody.msg)
        end
	elseif cmd.msghead.msgname == "sendmail_to_rids" then       --批量发送邮件
        if not cmd.msgbody or not cmd.msgbody.rids or not cmd.msgbody.fields then
            local f = io.open("data.lua","r")
            local source = nil
	        if f then
		        source = f:read "*a"
                f:close()
	        end
	        if source == nil then
		        return
	        end
	        local data = load(source,"error file!")()
            local msg = tabletool.deepcopy(data)
		    if msg and msg.rids then
			    msg.fields.attach_json = json.encode(msg.fields.attach_json)
			    for _,rid in pairs(msg.rids) do
				    cluster.send("gatenode", ".router", "newmail", tonumber(rid), msg.fields)
			    end
		    end
        else
            if type(cmd.msgbody.fields.attach_json) == "table" then
                cmd.msgbody.fields.attach_json = json.encode(cmd.msgbody.fields.attach_json)
                cmd.msgbody.fields.sendrid = 1
                cmd.msgbody.fields.sendname = cmd.return_id or "admin"
            end
            for _,rid in pairs(cmd.msgbody.rids) do
			    cluster.send("gatenode", ".router", "newmail", tonumber(rid), cmd.msgbody.fields)
			end
        end
	elseif cmd.msghead.msgname == "get_rid_tableinfo"       --获得玩家所在桌子信息
    or cmd.msghead.msgname == "clear_role_tableinfo"        --清除玩家桌子信息
    or cmd.msghead.msgname == "blocked_rid"                 --封号
    or cmd.msghead.msgname == "unblocked_rid" then          --解封
        local rid = cmd.msgbody.rid 
		local status = true
		if not rid then
			status,rid = redisdao.query_data(".gmmqredis", "lpop", "requestmq")
		end
        if status and rid then
		    result = cluster.call("gatenode", ".router", cmd.msghead.msgname,tonumber(rid))
        end
    elseif cmd.msghead.msgname == "set_gameend_flag" then   --停服
        local nodename = cmd.msgbody.nodename 
		local status = true
		if not nodename then
			status,nodename = redisdao.query_data(".gmmqredis", "lpop", "requestmq")
		end
        if status and nodename then
            if nodename == "*" then
                local nodenames = {"dnnode", "psznode", "hbslnode", "lhdnode", "brnnnode", "rbnode", "ttznode", "ddznode", "bjlnode", "bmnode"}
                for _,name in pairs(nodenames) do
                    cluster.call("gatenode", ".router", cmd.msghead.msgname,name)
                end
            else
		        cluster.call("gatenode", ".router", cmd.msghead.msgname,nodename)
            end
        end
    elseif cmd.msghead.msgname == "set_gamenode_config_ab" then --切换AB服
        local game = cmd.msgbody.game 
        local ab = cmd.msgbody.ab 
		local status = true
		if not ab or not game then
            status,game = redisdao.query_data(".gmmqredis", "lpop", "requestmq")
			status,ab = redisdao.query_data(".gmmqredis", "lpop", "requestmq")
		end
        if game == "*" then
            local nodenames = {"dn", "psz", "hbsl", "lhd", "brnn", "rb", "ttz", "ddz", "bjl", "bm"}
            for _,name in pairs(nodenames) do
                cluster.call("gatenode", ".router", cmd.msghead.msgname, name, ab)
            end
        else
		    cluster.call("gatenode", ".router", cmd.msghead.msgname, game, ab)
        end
	elseif cmd.msghead.msgname == "sethrlconfigmsg"  then       --设置静态跑马灯
         cluster.send("gatenode", ".router", cmd.msghead.msgname, cmd.msgbody.configmsg)
	elseif cmd.msghead.msgname == "get_blockedlist" then        --获得封号列表
		local status = false
		status,result = redisdao.query_data(".gmmqredis", "get", "blockedlist")
		if status and result then
			result = json.decode(result)
		end
    elseif cmd.msghead.msgname == "get_wait_rolelist"           --获得匹配玩家列表
    or cmd.msghead.msgname == "reset_matchqueue" then           --重置匹配队列
        local nodename = cmd.msgbody.nodename 
        local status = true
		if not nodename then
			status,nodename = redisdao.query_data(".gmmqredis", "lpop", "requestmq")
		end
        if status and nodename then
            result = cluster.call("gatenode", ".router", cmd.msghead.msgname, nodename)
        end
    elseif cmd.msghead.msgname == "getallhrlmsg" then           --获得所有动态跑马灯
        result = cluster.call("gatenode", ".router", cmd.msghead.msgname)
        redisdao.query_data(".gmmqredis", "set", "allhrlmsg",json.encode(result))
    elseif cmd.msghead.msgname == "delhrlmsg" then              --删除某条动态跑马灯
        cluster.send("gatenode", ".router", cmd.msghead.msgname, tonumber(cmd.msgbody.index), tonumber(cmd.msgbody.hrltype))
    elseif cmd.msghead.msgname == "setpaychannel" then          --设置充值渠道
        local paychannel = {business_bank_list = cmd.msgbody.business_bank_list, allpaychannel = cmd.msgbody.allpaychannel}
        local paychannel_str = json.encode(paychannel) or "{}"
        cluster.send("gatenode", ".router", cmd.msghead.msgname, paychannel_str)
        redisdao.query_data(".gmmqredis", "set", "allpaychannel", paychannel_str)
    elseif cmd.msghead.msgname == "simulate_client" then    --模拟客户端
        cluster.send("gatenode", ".router", cmd.msghead.msgname, cmd.msgbody.rid, cmd.msgbody.msgname, cmd.msgbody.msgbody)
    elseif cmd.msghead.msgname == "reduce_money" then       --扣钱
        cluster.send("gatenode", ".router", cmd.msghead.msgname, cmd.msgbody.rid, cmd.msgbody.money_type, cmd.msgbody.value)
    elseif cmd.msghead.msgname == "reduce_prop" then        --扣道具
        cluster.send("gatenode", ".router", cmd.msghead.msgname, cmd.msgbody.rid, cmd.msgbody.prop_id, cmd.msgbody.value)
    elseif cmd.msghead.msgname == "getpaychannel" then      --获得充值渠道信息
        local response = cluster.call("gatenode", ".router", cmd.msghead.msgname)
        redisdao.query_data(".gmmqredis", "set", "allpaychannel",response.paychannel)
    elseif cmd.msghead.msgname == "change_whitelist_state" then --修改白名单状态
        cluster.send("gatenode", ".router", cmd.msghead.msgname, cmd.msgbody.newstate)
    elseif cmd.msghead.msgname == "update_whitelist" then       --修改白名单
		cluster.send("gatenode", ".router", cmd.msghead.msgname)
	elseif cmd.msghead.msgname == "addpopmessage" then          --添加弹出式全服广播
		cmd.msgbody = cmd.msgbody.msg or ""
		cluster.send("gatenode", ".router", cmd.msghead.msgname, cmd.msgbody.msg)
	elseif cmd.msghead.msgname == "addjackoptnum" then          --增加奖池金额
		local game = cmd.msgbody.game
		if not game or (game ~= "dn" and game ~= "psz" and game ~= "gang28" and game ~= "sdb") then 
			filelog.sys_error("-----addjackoptnum-- wrong gamenode-----")
			return true
		end
		local gamenode = tostring(game) .. "node"
		result = cluster.call(gamenode, ".router", "addjackoptnum", cmd.msgbody.childid, cmd.msgbody.moneydata, cmd.msgbody.reason)
	elseif cmd.msghead.msgname == "deljackoptnum" then          --减少奖池金额
		local game = cmd.msgbody.game
		if not game or (game ~= "dn" and game ~= "psz" and game ~= "gang28" and game ~= "sdb") then 
			filelog.sys_error("------deljackoptnum-----wrong gamenode----")
			return false
		end
		local gamenode = tostring(game) .. "node"
		result = cluster.call(gamenode, ".router", "deljackoptnum", cmd.msgbody.childid, cmd.msgbody.moneydata, cmd.msgbody.reason)
	elseif cmd.msghead.msgname == "getsysperinfo" then          --获得系统奖池抽水比率
		local game = cmd.msgbody.game
		if not game or (game ~= "dn" and game ~= "psz" and game ~= "gang28" and game ~= "sdb") then 
			filelog.sys_error("-----getsysperinfo-- wrong gamenode-----")
			return true
		end
		local gamenode = tostring(game) .. "node"
		result = cluster.call(gamenode, ".router", "getsysperinfo", cmd.msgbody.chargename)
	elseif cmd.msghead.msgname == "changesysper" then           --修改系统奖池抽水比率
		local game = cmd.msgbody.game
		if not game or (game ~= "dn" and game ~= "psz" and game ~= "gang28" and game ~= "sdb") then 
			filelog.sys_error("-----changesysper-- wrong gamenode-----")
			return true
		end
		local gamenode = tostring(game) .. "node"
		result = cluster.call(gamenode, ".router", "changesysper", cmd.msgbody.chargename, cmd.msgbody.percent, cmd.msgbody.play_type)
    elseif cmd.msghead.msgname == "reload_ipdata" then              --重新加载ip数据库
        result = cluster.send("loginnode", ".router", "reload_ipdata", cmd.msgbody.is_all)
    elseif cmd.msghead.msgname == "set_static_inventory" then       --修改静态库存值   
		local gamename = cmd.msgbody.gamename
        local gametype = cmd.msgbody.gametype
        local value = cmd.msgbody.value
        if gamename and gametype and value then
            local key = tostring(gamename) .. "_" .. tostring(gametype) .. "_system_static_inventory"
            redisdao.query_data(".gmmqredis", "set", key, value)
        end
    elseif cmd.msghead.msgname == "update_weight_list" then         --修改开奖权值表
        local new_w_list = {}
        local gamename = cmd.msgbody.gamename
        if gamename and cmd.msgbody.weight_list then
            for k,v in pairs(cmd.msgbody.weight_list) do
                local k1 = tonumber(k)
                if type(v) == "table" then
                    new_w_list[k1] = { }
                    for kk,vv in pairs(v) do
                        local k2 = tonumber(kk)
                        new_w_list[k1][k2] = tonumber(vv)
                    end
                else
                    new_w_list[k1] = tonumber(v)
                end
            end
            cluster.send("gatenode", ".router", cmd.msghead.msgname, gamename, new_w_list)
        end
    elseif cmd.msghead.msgname == "set_system_rate" then        --修改系统税率
        local gamename = cmd.msgbody.gamename
        local gametype = cmd.msgbody.gametype
        local value = tonumber(cmd.msgbody.value)
        if gamename and gametype and value then
            if value > 0 and value < 1 then
                local key = tostring(gamename) .. "_" .. tostring(gametype) .. "_rate"
                redisdao.query_data(".gmmqredis", "set", key, value)
            end
        end
    elseif cmd.msghead.msgname == "set_service_fee_rate" then   --修改系统抽水比率
        local gamename = cmd.msgbody.gamename
        local gametype = cmd.msgbody.gametype
        local value = tonumber(cmd.msgbody.value)
        if gamename and gametype and value then
            if value > 0 and value < 1 then
                local key = tostring(gamename) .. "_" .. tostring(gametype) .. "_service_fee_rate"
                redisdao.query_data(".gmmqredis", "set", key, value)
            end
        end
    elseif cmd.msghead.msgname == "set_jackpot_rate" then       --修改奖池税率
        local gamename = cmd.msgbody.gamename
        local gametype = cmd.msgbody.gametype
        local value = tonumber(cmd.msgbody.value)
        if gamename and gametype and value then
            if value > 0 and value < 1 then
                local key = tostring(gamename) .. "_" .. tostring(gametype) .. "_jackpot_rate"
                redisdao.query_data(".gmmqredis", "set", key, value)
            end
        end
    elseif cmd.msghead.msgname == "set_system_lose_limit" then  --设置游戏输上限
        local gamename = cmd.msgbody.gamename
        local loselimit_base = tonumber(cmd.msgbody.loselimit_base)
        local loselimit_factor = tonumber(cmd.msgbody.loselimit_factor)
        if gamename and loselimit_base and loselimit_factor then
            cluster.send("gatenode", ".router", cmd.msghead.msgname, gamename, loselimit_base, loselimit_factor)
        end
    elseif cmd.msghead.msgname == "set_game_robot_num" then     --设置游戏内机器人数量
        local gamename = cmd.msgbody.gamename
        local base_num = tonumber(cmd.msgbody.base_num)
        local inc_factor = tonumber(cmd.msgbody.inc_factor)
        if gamename and base_num and inc_factor then
            cluster.send("gatenode", ".router", cmd.msghead.msgname, gamename, base_num, inc_factor, cmd.msgbody.min_num)
        end
    elseif cmd.msghead.msgname == "get_game_robot_state"    --获得游戏内机器人运行状态
    or cmd.msghead.msgname == "get_game_robot_num" then     --获得游戏内机器人数目
        local gamename = cmd.msgbody.gamename or "*"
        if gamename ~= "*" then
            result = cluster.call("gatenode", ".router", cmd.msghead.msgname, gamename)
            if cmd.msghead.msgname == "get_game_robot_state" then
                redisdao.query_data(".gmmqredis", "hset", "game_robot_state", gamename, result or false)
            else
                redisdao.query_data(".gmmqredis", "hset", "game_robot_num", gamename, result or 0)
            end
        else
            local allgames = { "lhd", "brnn", "rb", "ttz", "psz", "dn", "bjl", "hbsl", "yydb", "slwh"}
            for _,name in pairs(allgames) do
                local result = cluster.call("gatenode", ".router", cmd.msghead.msgname, name)
                if cmd.msghead.msgname == "get_game_robot_state" then
                    redisdao.query_data(".gmmqredis", "hset", "game_robot_state", name, result or false)
                else
                    redisdao.query_data(".gmmqredis", "hset", "game_robot_num", name, result or 0)
                end
            end
        end
    elseif cmd.msghead.msgname == "set_game_robot_state" then   --设置游戏内机器人运行状态
        local gamename = cmd.msgbody.gamename
        if gamename then
            if gamename == "*" then
                local allgames = { "lhd", "brnn", "rb", "ttz", "psz", "dn", "bjl", "hbsl"}
                for _,name in pairs(allgames) do
                    cluster.send("gatenode", ".router", cmd.msghead.msgname, name, cmd.msgbody.state)
                end
                
            else
                cluster.send("gatenode", ".router", cmd.msghead.msgname, gamename, cmd.msgbody.state)
            end
        end
    elseif cmd.msghead.msgname == "batch_build_robots" then     --批量创建机器人
        result = cluster.send("robotnode", ".router", "batch_build_robots", cmd.msgbody.num, cmd.msgbody.conf)
    elseif cmd.msghead.msgname == "get_robot_info" then         --获取机器人节点机器人数据
        result = cluster.call("robotnode", ".router", "get_robot_info")
    elseif cmd.msghead.msgname == "get_robot_inventory" then    --获得机器人库存
        result = cluster.call("robotnode", ".router", "get_robot_inventory")
    elseif cmd.msghead.msgname == "add_inventory" then          --增加机器人库存
        result = cluster.send("robotnode", ".router", "add_inventory", cmd.msgbody.moneytype, cmd.msgbody.value, cmd.msgbody.reason)
    elseif cmd.msghead.msgname == "sub_inventory" then          --减少机器人库存
        result = cluster.send("robotnode", ".router", "sub_inventory", cmd.msgbody.moneytype, cmd.msgbody.value, cmd.msgbody.reason)
    elseif cmd.msghead.msgname == "change_robot_state" then     --改变机器人节点运行状态
        result = cluster.send("robotnode", ".router", "change_robot_state", cmd.msgbody.state, cmd.msgbody.gamename)
    elseif cmd.msghead.msgname == "get_robot_state" then        --获得机器人节点运行状态
        result = cluster.call("robotnode", ".router", "get_robot_state")
        redisdao.query_data(".gmmqredis", "set", "robot_state", result or false)
    elseif cmd.msghead.msgname == "send_massmail" then          --发送群发邮件
        result = skynet.call(".massmail", "lua", "send_massmail", cmd.msgbody)
    elseif cmd.msghead.msgname == "del_massmail" then           --删除群发邮件
        result = skynet.call(".massmail", "lua", "del_massmail", cmd.msgbody.keyid)
    elseif cmd.msghead.msgname == "change_role_info" then       --修改玩家信息
        local roleinfos = {}
        for k,v in pairs(cmd.msgbody) do
            if k ~= "rid" then
                roleinfos[k] = v
            end
        end
        result = cluster.send("gatenode", ".router", "updateroleinfo", tonumber(cmd.msgbody.rid), roleinfos)
    elseif cmd.msghead.msgname == "del_mail" then               --删除邮件
        result = cluster.call("gatenode", ".router", "del_mail", tonumber(cmd.msgbody.rid), tonumber(cmd.msgbody.keyid))
    elseif cmd.msghead.msgname == "set_withdraw_alipay_state" then --设置支付宝提现状态
        local msg_str = nil
        if tonumber(cmd.msgbody.state) == 1 then
            msg_str = "{\"cmd\":1,\"TX_Alipy\":1}"
        else
            msg_str = "{\"cmd\":1,\"TX_Alipy\":0}"
        end

        local cmd_one = {
            msghead = { msgname = "sethrlconfigmsg",},
            msgbody = { configmsg = { [1] = msg_str, }, },
        }
        redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd_one))

        local cmd_two = {
            msghead = {msgname = "addhrlmsg",},
            msgbody = { msg = { level = 1, repeatnum = 1, content = msg_str, }, },
        }
        redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd_two))
    elseif cmd.msghead.msgname == "set_game_clientver" then --设置游戏客户端版本
        if type(cmd.msgbody.gamevers) == "table" then
            local msg_str = { cmd = 1, Game_Version = cmd.msgbody.gamevers }
            local cmd_one = {
                msghead = {msgname = "addhrlmsg",},
                msgbody = { msg = { level = 1, repeatnum = 1, content = json.encode(msg_str), }, },
            }
            redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd_one))
        end
	elseif cmd.msghead.msgname == "refresh_alipay_max_withdraw" then --刷新支付宝最大提现金额
        local withdraw_type = EWithdrawType.EWITHDRAW_TYPE_ALIPAY
        local _,today_withdraw_max_money = redisdao.query_data(".gmmqredis", "hget", "today_withdraw_max_money", withdraw_type)
        today_withdraw_max_money = tonumber(today_withdraw_max_money) or 100000
        local msg_str = { cmd = 1, TX_Alipy = 1 }
        local cmd_one = {
            msghead = {msgname = "addhrlmsg",},
            msgbody = { msg = { level = 1, repeatnum = 1, content = json.encode(msg_str), }, },
        }
        redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd_one))
        msg_str = { cmd = 1, alipay_max_money = today_withdraw_max_money }
        local cmd_two = {
            msghead = {msgname = "addhrlmsg",},
            msgbody = { msg = { level = 1, repeatnum = 1, content = json.encode(msg_str), }, },
        }
        redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd_two))
    elseif cmd.msghead.msgname == "set_open_givemoney_level" then   ---设置赠送可以使用的等级
        ---使用时先打开服务赠送开关  redis db 15 set givemoney_is_open 1
        local open_level = tonumber(cmd.msgbody.open_givemoney_level)
        redisdao.query_data(".gmmqredis", "set", "use_givemoney_level", open_level)
        local msg_str = { cmd = 1, open_givemoney_level = open_level }
        local cmd_one = {
            msghead = {msgname = "addhrlmsg",},
            msgbody = { msg = { level = 1, repeatnum = 1, content = json.encode(msg_str), }, },
        }
        redisdao.query_data(".gmmqredis", "rpush", "requestmq", json.encode(cmd_one))
    elseif cmd.msghead.msgname == "genfreeridpool" then    --生成rid池
        skynet.send(".router", "lua", "genfreeridpool")
    elseif cmd.msghead.msgname == "unlock_phonenum" then
        cluster.send("loginnode", ".router", "unlock_phonenum", tostring(cmd.msgbody.phonenum))
    elseif cmd.msghead.msgname == "change_phonenum" then    --修改手机号
        local rid = 0
        local id = cmd.msgbody.oldphone or ""
        local newphone = cmd.msgbody.newphone or ""
        local sqlstr = "select * from rs_user where phonenum =  " .. tostring(id)
        local isok,resul_data = skynet.pcall(cluster.call, "datanode", ".router", "mysqlquery", 1, sqlstr)
        if isok and resul_data and resul_data[1] and resul_data[2] and resul_data[2][1] then
            rid = tonumber(resul_data[2][1].rid)
        elseif tonumber(cmd.msgbody.rid) then
            rid = tonumber(cmd.msgbody.rid)
        else
            return false
        end
        local result1 = cluster.call("datanode", ".router", "update_rk", rid, "rs_user", nil, {phonenum = newphone})
        if not result1 or result1[1] == 0 then
            filelog.sys_error("手机号已存在", rid, id, newphone)
            return false
        end
        cluster.send("gatenode", ".router", "updateroleinfo", rid, {phonenum = newphone})
        return true
    elseif cmd.msghead.msgname == "change_player_level" then
        local rid = cmd.msgbody.rid
        local level = cmd.msgbody.level or 0
        local result1 = cluster.call("datanode", ".router", "update_rk", rid, "rs_info", nil, {level = level})
        if not result1 or result1[1] == 0 then
            ----filelog.sys_error("手机号已存在", rid, id, newphone)
            return false
        end
        cluster.send("gatenode", ".router", "updateroleinfo", rid, {level = level})
    elseif cmd.msghead.msgname == "refuse_save_robotinfo" then    --拒绝保存机器人
        result = cluster.send("robotnode", ".router", "refuse_save_robotinfo", cmd.msgbody.state)
    end

    --将结果保存到结果队列
    if cmd.id then
        result = result or true
        local response = {id = cmd.id, result = result}
        redisdao.query_data(".gmmqredis", "rpush", "responsemq",json.encode(response))
    elseif cmd.mid then
        local response = {cmd = cmd.msghead.msgname, result = result}
        redisdao.query_data(".gmmqredis", "rpush", "myrespmq",json.encode(response))
    end

    return result
end

skynet.start(function()
	skynet.call(".gmmqredis", "lua", "init", configdao.get_cfgsvrs_key("gmmqredis"))

	-- 就是循环休息1s 去读取一下 gmmqredis 的 request
	-- 如果读到消息了，就不休息，马上再循环
	skynet.fork(function()
		while true do
			local status, cmd = redisdao.query_data(".gmmqredis", "lpop", "requestmq")
			if status and cmd then
				status,result = pcall(docmd,cmd)
                if not status then
                    filelog.sys_error("docmd error!",cmd)
                end
                filelog.print("line 209", result)
			else
				skynet.sleep(100)
			end
		end
	end)

    --每隔5分钟，读取一次在线玩家数
    skynet.fork(function()
		while true do
			local status,result = pcall(docmd,"get_online_state")
            if type(result) == "table" then
                result.curtime = timetool.get_time()
                status,result.robotinfo = pcall(docmd, "get_robot_info")
                if not status then result.robotinfo = nil end
                redisdao.query_data(".gmmqredis", "lpush", "onlinedata",json.encode(result))
            end
			skynet.sleep(2000)
		end
	end)	

end)
